#-*- coding: utf-8 -*-

from django.contrib.auth.models import User
from django.db import models
# Create your models here.

RESULT_CHOICES = (
    ('WT', 'Waiting'),
    ('AC','STATUS OK'),
    ('PE', 'Presentation Error'),
    ('WA', 'Wrong Answer'),
    ('RE', 'Runtime Error'),
    ('TE', 'Time Limit Exceed'),
    ('ME', 'Memory Limit Exceed'),
    ('OE', 'Output Limit Exceed'),
    ('CE', 'Compile Error'),
    ('SE', 'System Error'),
    ('VE', 'Validate Error'),
    )

LANGUAGE_CHOICES = (
    ('c', 'C'),
    ('cpp', 'C++'),
    ('py', 'Python'),
    ('java', 'Java'),
    ('c#', 'C#'),
    ('lua', 'Lua'),
    ('pas', 'Pascal')
    #('f#', 'F#'),
    )

class UserProfile(models.Model):
    user = models.OneToOneField(User, primary_key=True)
    nick = models.CharField(max_length = 100, blank = True)
    major = models.CharField(max_length = 100, blank = True)
    school = models.CharField(max_length = 100, blank = True)
    email = models.EmailField(blank = True)
    submit = models.IntegerField(default = 0)
    solved = models.IntegerField(default = 0)
    defunct = models.BooleanField(default=False)
    
    def __unicode__(self):
        return self.user.username
    
    class Meta:
        db_table = 'judge_userprofile'

class Problem(models.Model):
    problem_id = models.AutoField(primary_key = True)
    title = models.CharField(max_length = 200)
    description = models.TextField(blank=True)
    input = models.TextField(blank=True)
    output = models.TextField(blank=True)
    input_path = models.CharField(max_length = 255, blank=True)
    output_path = models.CharField(max_length = 255, blank=True)
    sample_input = models.TextField(blank=True)
    sample_output = models.TextField(blank=True)
    hint = models.TextField(blank=True)
    source = models.CharField(max_length = 100, blank=True)
    in_date = models.DateTimeField(blank=True)
    time_limit = models.IntegerField(default = 1000)
    case_time_limit = models.IntegerField(default = 0)
    memory_limit = models.IntegerField(default = 1024)
    defunct = models.BooleanField(default = False)
    accepted = models.IntegerField(default = 0)
    submit = models.IntegerField(default = 0)
    ratio = models.DecimalField(max_digits=4, decimal_places=2, default = 0)
    submit_user = models.IntegerField(default = 0)
    solved = models.IntegerField(default = 0)
    
    def __unicode__(self):
        return str(self.problem_id) + '.' + self.title
    
    class Meta:
        ordering = ['problem_id']
        db_table = 'judge_problem'

class Source_code(models.Model):
    source = models.TextField()
    def __unicode__(self): return str(self.id)

    class Meta:
        db_table = 'judge_source_code'

class Compileinfo(models.Model):
    error = models.TextField()
    
    class Meta:
        db_table = 'judge_compileinfo'

class Solution(models.Model):
    solution_id = models.AutoField(primary_key = True)
    problem_id = models.ForeignKey(Problem)
    user_id = models.ForeignKey(User)
    time = models.IntegerField(default = 0, blank = True)
    memory = models.IntegerField(default = 0, blank = True)
    in_date = models.DateTimeField(blank = True)
    result = models.CharField(max_length = 2, choices = RESULT_CHOICES)
    language = models.CharField(max_length = 4, choices = LANGUAGE_CHOICES)
    ip = models.IPAddressField(blank = True)
    #contest_id = models.ForeignKey(Contest)
    #valid = models.BooleanField()
    source_code = models.ForeignKey(Source_code, unique = True)
    compileinfo = models.ForeignKey(Compileinfo, blank = True, null=True)
    code_length = models.IntegerField(default = 0)
    
    def __unicode__(self): return str(self.solution_id)

    class Meta:
        ordering = ['-solution_id']
        db_table = 'judge_solution'

class Contest(models.Model):
    contest_id = models.AutoField(primary_key = True)
    title = models.CharField(max_length = 255)
    start_time = models.DateTimeField(blank = True)
    end_time = models.DateTimeField(blank = True)
    defunct = models.BooleanField(default = False)
    description = models.TextField(blank = True)
    private = models.BooleanField(default = False)
    problem = models.ManyToManyField(Problem)
    # more privilege parameter
    # cache contest time
    def __unicode__(self):
        return self.title
    
    class Meta:
        ordering = ['-solution_id']
        db_table = 'judge_contest'
        
    